﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using DataModel;
using System.Linq;
using A_ManageUsers.Services.Security;
namespace A_ManageUsers.DataModels
{
    public class UserRole:DataModelBase
    {
        public static ObservableCollection<UserRole> GetUserRoles(string UserName, int[] Ros)
        {
            return new ObservableCollection<UserRole>(Roles.RolesL.Select((R) => new UserRole(UserName, Ros.Contains(R.ID), R)));
        }

        public UserRole(string Username, bool Enabled, Role Role)
        {
            this.Username = Username;
            this._Enabled = Enabled;
            this.originalEnabled = Enabled;
            R = Role;
            R.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(R_PropertyChanged);
        }
        Role R;
        void R_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "ID")
            {
                OnPropertyChanged("RoleID");
            }
            else if (e.PropertyName == "Name")
            {
                OnPropertyChanged("RoleName");
            }
        }

        public string Username { get; set; }

        private bool originalEnabled;
        private bool _Enabled;
        public bool Enabled
        {
            get { return _Enabled; }
            set
            {
                if (_Enabled != value)
                {
                    base.SetRevertValue("Enabled", _Enabled);
                    _Enabled = value;
                    base.SetComitAction("Enabled", value, (DMB, PN, V) =>
                        {
                            UserManagementClient UMC = ServiceConfig.NewUserManagementClient();
                            if (value == originalEnabled)
                            { 
                                //In the event that the value hasn't changed we don't want to bother sending anything to the server.
                            }
                            else if (value)
                            {
                                UMC.AddRoleToUserAsync(Username, RoleID);
                            }
                            else
                            {
                                UMC.RemoveRoleFromUserAsync(Username, RoleID);
                            }
                            originalEnabled = value;
                        });
                    OnPropertyChanged("Enabled");
                }
            }
        }

        public int RoleID { get { return R.ID; } }
        public string RoleName { get { return R.Name; } }

        protected override void ComitNewObject()
        {
            
        }
    }
}
